# Build recipe for xorg-server.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=xorg-server
version=1.20.5
release=5

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/xorg/xserver"

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=http://www.x.org/releases/individual/xserver/$tarname

description="
A portable, network-transparent Window System.

The X Window System is a network transparent window system which
runs on a wide range of computing and graphics machines.
"

homepage=http://www.x.org
license="MIT X Consortium"

# Source documentation
docs="COPYING ChangeLog README.md"
docsdir="${docdir}/${program}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Fix compilation under ARM (Thanks to "Alpine Linux")
    patch -Np1 -i "${worktree}/patches/xserver/fix-musl-arm.patch"

    # Adjust headers
    sed -i '/^#include <asm\/page.h>/d' Xext/xf86bigfont.c
    sed -i 's#termio.h#termios.h#' hw/xfree86/os-support/xf86_OSlib.h

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS -Wl,-z,lazy" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-xf86bigfont \
     --enable-xcsecurity \
     --enable-kdrive \
     --enable-config-udev \
     --enable-glamor \
     --enable-dri \
     --enable-dri2 \
     --enable-dri3 \
     --enable-ipv6 \
     --enable-suid-wrapper \
     --disable-systemd-logind \
     --with-pic \
     --with-int10=x86emu \
     --with-sha1=libgcrypt \
     --with-default-font-path="/usr/share/fonts/X11/misc,/usr/share/fonts/X11/Type1,/usr/share/fonts/X11/100dpi:unscaled,/usr/share/fonts/X11/75dpi:unscaled,/usr/share/fonts/X11/cyrillic,/usr/share/fonts/X11/TTF,/usr/share/fonts/X11/OTF" \
     --with-module-dir=/usr/lib${libSuffix}/xorg/modules \
     --with-xkb-path=/etc/X11/xkb \
     --with-xkb-output=/var/lib/xkb \
     --with-os-name="GNU/Linux-Libre" \
     --with-os-vendor="Dragora"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install-strip

    # Allow old behavior to terminate server keystroke
    cp -p "${worktree}/archive/xserver/00-keyboard.conf" \
          "${destdir}/usr/share/X11/xorg.conf.d"
    chmod 644 "${destdir}/usr/share/X11/xorg.conf.d/00-keyboard.conf"

    # Set the suid bit on the Xorg application.
    #
    # This will allow us to switch between tty1 and tty7 with the
    # Ctrl-Alt-F1 and Ctrl-Alt-F7 key combinations (see
    # recipes/xorg/app/xinit/recipe).  Thanks to BLFS!
    chmod 4755 "${destdir}/usr/libexec/Xorg"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

